﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.util.*;
    import flash.ui.*;

    public class CursorManager {

        private static var managers:WeakMap = new WeakMap();

        protected var root:DisplayObjectContainer = null;
        protected var cursorHolder:DisplayObjectContainer = null;
        protected var currentCursor:DisplayObject = null;
        private var tiggerCursorMap:Dictionary;

        public function CursorManager(_arg1:DisplayObjectContainer){
            this.tiggerCursorMap = new Dictionary(true);
            super();
            this.setCursorContainerRoot(_arg1);
        }
        public static function getManager(_arg1:Stage=null):CursorManager{
            if (_arg1 == null){
                _arg1 = AsWingManager.getStage();
            };
            if (_arg1 == null){
                return (null);
            };
            var _local2:CursorManager = managers.getValue(_arg1);
            if (_local2 == null){
                _local2 = new CursorManager(_arg1);
                managers.put(_arg1, _local2);
            };
            return (_local2);
        }

        protected function setCursorContainerRoot(_arg1:DisplayObjectContainer):void{
            if (_arg1 != this.root){
                if (this.root){
                    this.root.removeEventListener(Event.DEACTIVATE, this.__referenceEvent);
                };
                this.root = _arg1;
                this.root.addEventListener(Event.DEACTIVATE, this.__referenceEvent);
                if (((!((this.cursorHolder == null))) && (!((this.cursorHolder.parent == this.root))))){
                    this.root.addChild(this.cursorHolder);
                };
            };
        }
        private function __referenceEvent(_arg1:Event):void{
        }
        protected function getCursorContainerRoot():DisplayObjectContainer{
            return (this.root);
        }
        public function showCustomCursor(_arg1:DisplayObject, _arg2:Boolean=true):void{
            var _local3:DisplayObjectContainer;
            if (_arg2){
                Mouse.hide();
            } else {
                Mouse.show();
            };
            if (_arg1 == this.currentCursor){
                return;
            };
            _local3 = this.getCursorContainerRoot();
            if (this.cursorHolder == null){
                if (_local3 != null){
                    this.cursorHolder = new Sprite();
                    this.cursorHolder.mouseEnabled = false;
                    this.cursorHolder.tabEnabled = false;
                    this.cursorHolder.mouseChildren = false;
                    _local3.addChild(this.cursorHolder);
                };
            };
            if (this.cursorHolder != null){
                if (this.currentCursor != _arg1){
                    if (this.currentCursor != null){
                        this.cursorHolder.removeChild(this.currentCursor);
                    };
                    this.currentCursor = _arg1;
                    this.cursorHolder.addChild(this.currentCursor);
                };
                DepthManager.bringToTop(this.cursorHolder);
                _local3.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.__mouseMove, false, 0, true);
                this.__mouseMove(null);
            };
        }
        private function __mouseMove(_arg1:MouseEvent):void{
            this.cursorHolder.x = this.cursorHolder.parent.mouseX;
            this.cursorHolder.y = this.cursorHolder.parent.mouseY;
            DepthManager.bringToTop(this.cursorHolder);
        }
        public function hideCustomCursor(_arg1:DisplayObject):void{
            if (_arg1 != this.currentCursor){
                return;
            };
            if (this.cursorHolder != null){
                if (this.currentCursor != null){
                    this.cursorHolder.removeChild(this.currentCursor);
                };
            };
            this.currentCursor = null;
            Mouse.show();
            var _local2:DisplayObjectContainer = this.getCursorContainerRoot();
            if (_local2 != null){
                _local2.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__mouseMove);
            };
        }
        public function setCursor(_arg1:InteractiveObject, _arg2:DisplayObject):void{
            this.tiggerCursorMap[_arg1] = _arg2;
            if (_arg2 != null){
                _arg1.addEventListener(MouseEvent.ROLL_OVER, this.__triggerOver, false, 0, true);
                _arg1.addEventListener(MouseEvent.ROLL_OUT, this.__triggerOut, false, 0, true);
                _arg1.addEventListener(MouseEvent.MOUSE_UP, this.__triggerUp, false, 0, true);
            } else {
                _arg1.removeEventListener(MouseEvent.ROLL_OVER, this.__triggerOver, false);
                _arg1.removeEventListener(MouseEvent.ROLL_OUT, this.__triggerOut, false);
                _arg1.removeEventListener(MouseEvent.MOUSE_UP, this.__triggerUp, false);
                delete this.tiggerCursorMap[_arg1];
            };
        }
        private function __triggerOver(_arg1:MouseEvent):void{
            var _local2:Object = _arg1.currentTarget;
            var _local3:DisplayObject = (this.tiggerCursorMap[_local2] as DisplayObject);
            if (((_local3) && (!(_arg1.buttonDown)))){
                this.showCustomCursor(_local3);
            };
        }
        private function __triggerOut(_arg1:MouseEvent):void{
            var _local2:Object = _arg1.currentTarget;
            var _local3:DisplayObject = (this.tiggerCursorMap[_local2] as DisplayObject);
            if (_local3){
                this.hideCustomCursor(_local3);
            };
        }
        private function __triggerUp(_arg1:MouseEvent):void{
            var _local2:InteractiveObject = (_arg1.currentTarget as InteractiveObject);
            var _local3:DisplayObject = (this.tiggerCursorMap[_local2] as DisplayObject);
            if (((_local3) && (_local2.hitTestPoint(_arg1.stageX, _arg1.stageY, true)))){
                this.showCustomCursor(_local3);
            };
        }

    }
}//package org.aswing 
